
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="zh_cn">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <title>4.23.&nbsp;&#35299;&#26512; XML [&#28145;&#20837;&#27973;&#20986; Greasemonkey]</title>
      <link rel="shortcut icon" href="/favicon.ico">
      <link rel="stylesheet" href="../css/dig.css" type="text/css">
      <meta http-equiv="Link" content='&lt;../css/modern.css&gt;; type="text/css"; rel=stylesheet, &lt;../css/empty.css&gt;; type="text/css";
      rel=stylesheet'>
      <link rev="made" href="mailto:mark@diveintomark.org">
      <meta name="generator" content="DocBook XSL Stylesheets V1.68.1">
      <meta name="keywords" content="Firefox, Greasemonkey, Javascript, user script, userscript">
      <link rel="start" href="../toc/index.html" title="&#28145;&#20837;&#27973;&#20986; Greasemonkey">
      <link rel="up" href="index.html" title="&#31532;&nbsp;4&nbsp;&#31456;&nbsp;&#20844;&#20849;&#27169;&#24335;">
      <link rel="prev" href="override-method.html" title="4.22.&nbsp;&#35206;&#30422;&#20869;&#24314;&#30340; Javascript &#26041;&#27861;">
      <link rel="next" href="../casestudy/index.html" title="&#31532;&nbsp;5&nbsp;&#31456;&nbsp;&#23454;&#20363;&#25945;&#23398;">
   </head>
   <body id="diveintogreasemonkey-org" class="pattern-parsexml">
      <div class="z" id="intro">
         <div class="sectionInner">
            <div class="sectionInner2">
               <div class="s">
                  <h1><a href="/" accesskey="1">&#28145;&#20837;&#27973;&#20986; Greasemonkey</a></h1>
                  <p>&#25945;&#32769;&#32593;&#32476;&#23398;&#26032;&#25226;&#25103;</p>
               </div>
               <div class="s">
                  <ul>
                     <li><a href="../">&#36215;&#22987;&#39029;</a> &middot; 
                     </li>
                     <li><a href="../toc/">&#30446;&#24405;</a> &middot; 
                     </li>
                     <li><a href="../download/">&#19979;&#36733;</a> &middot; 
                     </li>
                     <li><a href="http://greasemonkey.mozdev.org/">&#31435;&#21363;&#19979;&#36733; Greasemonkey</a></li>
                  </ul>
               </div>
            </div>
         </div>
      </div>
      <div id="main">
         <div id="mainInner">
            <p id="breadcrumb">&#24744;&#30340;&#20301;&#32622;&#65306;<a href="../">&#36215;&#22987;&#39029;</a> &#8594; <a href="../toc/index.html">&#30446;&#24405;</a> &#8594; <a href="index.html">&#20844;&#20849;&#27169;&#24335;</a> &#8594; <span class="thispage">&#35299;&#26512; XML</span></p>
            <div class="section" lang="zh_cn">
               <div class="titlepage">
                  <div>
                     <div>
                        <h2 class="title"><a name="pattern.parsexml" class="skip" href="#pattern.parsexml" title="link to this section"><img src="../images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> 4.23.&nbsp;&#35299;&#26512; <acronym title="Extensible Markup Language">XML</acronym></h2>
                     </div>
                     <div>
                        <div class="abstract">
                           <h3 class="title"></h3>
                           <p>Firefox &#33258;&#21160;&#23558;&#24403;&#21069;&#39029;&#38754;&#35299;&#26512;&#20026; DOM&#65292;&#20294;&#26159;&#24744;&#20063;&#21487;&#20197;&#25163;&#21160;&#21019;&#24314; DOM &#32780;&#19981;&#20351;&#29992;&#20219;&#20309; <acronym title="Extensible Markup Language">XML</acronym> &#23383;&#31526;&#20018;&#65292;&#21487;&#20197;&#26159;&#24744;&#33258;&#24049;&#24314;&#31435;&#30340;&#65292;&#20063;&#21487;&#20197;&#26159;&#24744;&#20174;&#36828;&#31471;&#20027;&#26426;&#24471;&#21040;&#30340; <acronym title="Extensible Markup Language">XML</acronym>&#12290;
                           </p>
                        </div>
                     </div>
                  </div>
               </div>
               <div class="example"><a name="example.pattern.parsexml.string" class="skip" href="#example.pattern.parsexml.string" title="link to this example"><img src="../images/permalink.gif" alt="[link]" title="link to this example" width="8" height="9"></a> 
                  <h3 class="title">&#20363;&nbsp;4.27.&nbsp;&#23558;&#20219;&#24847;&#23383;&#31526;&#20018;&#35299;&#26512;&#20026; <acronym title="Extensible Markup Language">XML</acronym></h3><pre class="programlisting ">var xmlString = '&lt;passwd&gt;' + 
	'  &lt;user id="101"&gt;' +
	'    &lt;login&gt;mark&lt;/login&gt;' + 
	'    &lt;group id="100"/&gt;' +
	'    &lt;displayname&gt;Mark Pilgrim&lt;/displayname&gt;' + 
	'    &lt;homedir&gt;/home/mark/&lt;/homedir&gt;' +
	'    &lt;shell&gt;/bin/bash&lt;/shell&gt;' +
	'  &lt;/user&gt;' +
	'&lt;/passwd&gt;'
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlString, "application/xml");</pre><p>&#36825;&#37324;&#20851;&#38190;&#30340;&#22320;&#26041;&#26159; <code class="classname">DOMParser</code> &#23545;&#35937;&#65292;&#26377;&#20010; <code class="methodname">parseFromString</code> &#26041;&#27861;&#12290;(&#23427;&#20063;&#26377;&#20854;&#20182;&#30340;&#26041;&#27861;&#65292;&#20294;&#22312;&#36825;&#37324;&#25105;&#20204;&#29992;&#19981;&#21040;&#12290;)<code class="methodname">parseFromString</code> &#26041;&#27861;&#26377;&#20004;&#20010;&#21442;&#25968;&#65306;&#35299;&#26512;&#29992;&#30340; <acronym title="Extensible Markup Language">XML</acronym> &#23383;&#31526;&#20018;&#65292;&#21644;&#20869;&#23481;&#31867;&#22411;&#12290;&#20004;&#32773;&#37117;&#26159;&#24517;&#38656;&#30340;&#12290;
                  </p>
               </div>
               <div class="note">
                  <table class="admon" summary="">
                     <tr>
                        <td class="admonicon"><a href="../appendix/tips.html" title="&#25216;&#24039;&#28165;&#21333;"><img alt="[&#27880;&#24847;]" src="../images/note.png" width="24" height="24" title=""></a></td>
                        <td class="admontext">
                           <p><code class="classname">DOMParser</code> &#30340; <code class="methodname">parseFromString</code> &#26041;&#27861;&#23558;&#20869;&#23481;&#30340;&#31867;&#22411;&#20316;&#20026;&#23427;&#30340;&#31532;&#20108;&#20010;&#21442;&#25968;&#12290;&#36825;&#20010;&#26041;&#27861;&#21487;&#20197;&#25509;&#21463; <code class="systemitem">application/xml</code>, <code class="systemitem">application/xhtml+xml</code> &#21644; <code class="systemitem">text/xml</code>&#12290;&#30001;&#20110;&#28145;&#31350;&#36215;&#26469;&#65292;&#29702;&#30001;&#20250;&#24456;&#33618;&#21776;&#65292;&#24744;&#24212;&#35813;&#22987;&#32456;&#20351;&#29992; <code class="systemitem">application/xml</code>&#12290;
                           </p>
                        </td>
                     </tr>
                  </table>
               </div>
               <p>&#36825;&#20010;&#27169;&#24335;&#38750;&#24120;&#24378;&#22823;&#65292;&#22914;&#26524;&#25226;&#23427;&#19982; <code class="function">GM_xmlhttpRequest</code> &#20989;&#25968;&#32467;&#21512;&#36215;&#26469;&#35299;&#26512;&#36828;&#31243;&#28304;&#30340; <acronym title="Extensible Markup Language">XML</acronym>&#12290;
               </p>
               <div class="example"><a name="example.pattern.parsexml.url" class="skip" href="#example.pattern.parsexml.url" title="link to this example"><img src="../images/permalink.gif" alt="[link]" title="link to this example" width="8" height="9"></a> 
                  <h3 class="title">&#20363;&nbsp;4.28.&nbsp;&#35299;&#26512;&#36828;&#31243;&#28304;&#30340; <acronym title="Extensible Markup Language">XML</acronym></h3><pre class="programlisting ">GM_xmlhttpRequest({
	method: 'GET',
	url: 'http://greaseblog.blogspot.com/atom.xml',
	headers: {
	'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey/0.3',
	'Accept': 'application/atom+xml,application/xml,text/xml',
	},
	onload: function(responseDetails) {
		var parser = new DOMParser();
		var dom = parser.parseFromString(responseDetails.responseText,
		"application/xml");
		var entries = dom.getElementsByTagName('entry');
		var title;
		for (var i = 0; i &lt; entries.length; i++) {
		title = entries[i].getElementsByTagName('title')[0].textContent;
		alert(title);
		}
	}
});</pre><p>&#36825;&#27573;&#20195;&#30721;&#20250;&#36733;&#20837;<a href="http://greaseblog.blogspot.com/atom.xml">http://greaseblog.blogspot.com/atom.xml</a>&#30340; Atom feed&#65292;&#25226;&#23427;&#35299;&#26512;&#20026; DOM&#65292;&#28982;&#21518;&#26597;&#35810; DOM &#24471;&#21040;&#26465;&#30446;&#21015;&#34920;&#12290;&#20877;&#23545;&#27599;&#20010;&#26465;&#30446;&#20570; DOM &#26597;&#35810;&#65292;&#33719;&#24471;&#26465;&#30446;&#30340;&#26631;&#39064;(title)&#65292;&#28982;&#21518;&#22312;&#23545;&#35805;&#26694;&#20013;&#26174;&#31034;&#20986;&#26469;&#12290;
                  </p>
               </div>
               <div class="seealso">
                  <h3>&#21442;&#35265;</h3>
                  <ul>
                     <li>
                        <a href="iterate-one-element.html" title="4.5.&nbsp;&#25805;&#20316;&#29305;&#23450; HTML &#20803;&#32032;&#30340;&#25152;&#26377;&#23454;&#20363;">&#25805;&#20316;&#29305;&#23450; <acronym title="HyperText Markup Language">HTML</acronym> &#20803;&#32032;&#30340;&#25152;&#26377;&#23454;&#20363;</a>
                        
                     </li>
                     <li>
                        <a href="../api/gm_xmlhttprequest.html" title="GM_xmlhttpRequest"><span class="refentrytitle">GM_xmlhttpRequest</span></a>
                        
                     </li>
                  </ul>
               </div>
            </div>
            <div style="float: left">&#8592;&nbsp;<a class="NavigationArrow" href="override-method.html">&#35206;&#30422;&#20869;&#24314;&#30340; Javascript &#26041;&#27861;</a></div>
            <div style="text-align: right"><a class="NavigationArrow" href="../casestudy/index.html">&#23454;&#20363;&#25945;&#23398;</a>&nbsp;&#8594;
            </div>
            <hr style="clear:both">
            <div class="footer">
               <p class="copyright">&#29256;&#26435; &copy; 2005 Mark Pilgrim &middot; <a title="&#21457;&#36865;&#32473;&#25105;&#26377;&#20110;&#27492;&#20070;&#30340;&#21453;&#39304;&#24847;&#35265;" href="mailto:mark@diveintomark.org">mark@diveintomark.org</a> &middot; <a href="../license/gpl.html" title="GNU &#36890;&#29992;&#20844;&#20849;&#35768;&#21487;&#35777;">&#20351;&#29992;&#26465;&#27454;</a></p>
            </div>
         </div>
      </div>
   </body>
</html>